{{- define "additional_parameters" }}
  {{- if .nodeFilter }}
nodeFilter:
  {{- .nodeFilter | toYaml | nindent 2 }}
    {{- end }}

    {{- if .priorityFilter }}
priorityFilter:
  {{- .priorityFilter | toYaml | nindent 2 }}
    {{- end }}

    {{- if .namespaceFilter }}
namespaceFilter:
  {{- .namespaceFilter | toYaml | nindent 2 }}
    {{- end }}

    {{- if .nodeFit }}
nodeFit:
  {{- .nodeFit | toYaml | nindent 2 }}
    {{- end }}
  {{- end }}

  {{- range $d := .Values.descheduler.internal.deschedulers }}
  {{- $deschedulerSpec := $d.spec }}
  {{- $globalParameters := $deschedulerSpec.deschedulerPolicy.globalParameters }}
  {{- $strategies := $deschedulerSpec.deschedulerPolicy.strategies }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: descheduler-policy-{{ $d.metadata.name | sha1sum }}
  namespace: d8-descheduler
  {{- include "helm_lib_module_labels" (list $) | nindent 2 }}
data:
  policy.yaml: |
    apiVersion: "descheduler/v1alpha1"
    kind: "DeschedulerPolicy"
  {{- if $globalParameters }}
    {{- $globalParameters | toYaml | nindent 4 }}
  {{- end }}
    strategies:
  {{- if dig "removeDuplicates" "enabled" false $strategies }}
      "RemoveDuplicates":
        enabled: true
        params:
    {{- include "additional_parameters" $strategies.removeDuplicates | nindent 10 }}
  {{- else }}
      "RemoveDuplicates":
        enabled: false
  {{- end }}

  {{- if dig "removePodsViolatingNodeAffinity" "enabled" false $strategies }}
      "RemovePodsViolatingNodeAffinity":
        enabled: true
        params:
          nodeAffinityType:
            - "requiredDuringSchedulingIgnoredDuringExecution"
    {{- include "additional_parameters" $strategies.removePodsViolatingNodeAffinity | nindent 10 }}
  {{- else }}
      "RemovePodsViolatingNodeAffinity":
        enabled: false
  {{- end }}

  {{- if dig "removePodsViolatingInterPodAntiAffinity" "enabled" false $strategies }}
      "RemovePodsViolatingInterPodAntiAffinity":
        enabled: true
        params:
    {{- include "additional_parameters" $strategies.removePodsViolatingInterPodAntiAffinity | nindent 10 }}
  {{- else }}
      "RemovePodsViolatingInterPodAntiAffinity":
        enabled: false
  {{- end }}


  {{- if dig "lowNodeUtilization" "enabled" false $strategies }}
      "LowNodeUtilization":
        enabled: true
        params:
          nodeResourceUtilizationThresholds:
            thresholds:
              "cpu": 40
              "memory": 50
              "pods": 40
            targetThresholds:
              "cpu": 80
              "memory": 90
              "pods": 80
    {{- include "additional_parameters" $strategies.lowNodeUtilization | nindent 10 }}
  {{- else }}
      "LowNodeUtilization":
        enabled: false
  {{- end }}

  {{- if dig "highNodeUtilization" "enabled" false $strategies }}
      "HighNodeUtilization":
        enabled: true
        params:
          nodeResourceUtilizationThresholds:
            thresholds:
              "cpu": 50
              "memory": 50
    {{- include "additional_parameters" $strategies.highNodeUtilization | nindent 10 }}
  {{- else }}
      "HighNodeUtilization":
        enabled: false
  {{- end }}

  {{- if dig "removePodsViolatingNodeTaints" "enabled" false $strategies }}
      "RemovePodsViolatingNodeTaints":
        enabled: true
        params:
    {{- include "additional_parameters" $strategies.removePodsViolatingNodeTaints | nindent 10 }}
  {{- else }}
      "RemovePodsViolatingNodeTaints":
        enabled: false
  {{- end }}

  {{- if dig "removePodsViolatingTopologySpreadConstraint" "enabled" false $strategies }}
      "RemovePodsViolatingTopologySpreadConstraint":
        enabled: true
        params:
    {{- include "additional_parameters" $strategies.removePodsViolatingTopologySpreadConstraint | nindent 10 }}
  {{- else }}
      "RemovePodsViolatingTopologySpreadConstraint":
        enabled: false
  {{- end }}

  {{- if dig "removePodsHavingTooManyRestarts" "enabled" false $strategies }}
      "RemovePodsHavingTooManyRestarts":
        enabled: true
        params:
          podsHavingTooManyRestarts:
            podRestartThreshold: 100
            includingInitContainers: true
    {{- include "additional_parameters" $strategies.removePodsHavingTooManyRestarts | nindent 10 }}
  {{- else }}
      "RemovePodsHavingTooManyRestarts":
        enabled: false
  {{- end }}

  {{- if dig "podLifeTime" "enabled" false $strategies }}
      "PodLifeTime":
        enabled: true
        params:
          podLifeTime:
            maxPodLifeTimeSeconds: 86400
            podStatusPhases:
              - "Pending"
  {{- else }}
      "PodLifeTime":
        enabled: false
  {{- end }}
{{- end }}
